﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Html;
using Quicklight.Web.Regions;

namespace Quicklight.Web {

   /// <summary>
   /// The AppBase class is an abstract class which an application must extend to supply an instance to the Run entry method.
   /// To extend the AppBase class override the InitializeModules to initialise the modules and override the GetRootView to specify the initial view to load.
   /// During the initialisation of the modules the services, models and views should be registerd with the ObjectManager as well as any other configuration of the application.
   /// The GetRootView should return the initial view to display on startup.
   /// The instance of the extended AppBase supplied to the static Run method is available from the static property Instance.
   /// The root container of the application is available as a property on the instance of the extended AppBase.
   /// </summary>
   public abstract class AppBase {
      private static AppBase instance;
      private Container container;

      /// <summary>
      /// Construct an instance of the AppBase with a container having the default namespace and default name.
      /// If the root view is a BrowserView, the default container is the body element of the page.
      /// </summary>
      public AppBase() {
         this.container = new Container("", "");
      }

      /// <summary>
      /// Construct an instance of the AppBase with a container having the default namespace and specified name.
      /// If the root view is a BrowserView, the container will be the element which has the id equal to the containerName
      /// </summary>
      /// <param name="containerName">Name of the container in which to show the initial view</param>
      public AppBase(string containerName) {
         this.container = new Container(containerName, "");
      }

      /// <summary>
      /// Returns the instance of the AppBase which was supplied to the static Run method on startup.
      /// </summary>
      public static AppBase Instance {
         get { return instance; }
      }

      /// <summary>
      /// Returns the root container of the AppBase.
      /// The initial view is displayed in this container.
      /// Use the container to show views at the highest level
      /// </summary>
      public Container Container {
         get { return container; }
      }

      /// <summary>
      /// The override of the InitializeModules method should do the initialisation of the application. 
      /// Use this override to register types and instances (such as view, viewmodels, models and services) with the ObjectManager
      /// If the application is to be used on platforms with different screen sizes, use this method to load the appropriate views for the detected device
      /// </summary>
      public abstract void InitializeModules();

      /// <summary>
      /// The override of the GetRootView method should return the initial view of the application
      /// </summary>
      /// <returns>The initial view to display in the root container</returns>
      public abstract IViewable GetRootView();

      /// <summary>
      /// Entry point of the application. The supplied AppBase instance is stored, the InitializeModules method is called, the view supplied by GetRootView is then shown in the container of the AppBase
      /// Call this method from the webpage containing the application, eg the load event of the body element
      /// </summary>
      /// <param name="app">Instance of a type that extends the Appbase class</param>
      public static void Run(AppBase app) {
         instance = app;
         Instance.InitializeModules();
         IViewable rootView = Instance.GetRootView();
         Instance.Container.Show(rootView);
      }
   }
}
